//
//  FTBannerItem.m
//  FashionTech
//
//  Created by JackieYip on 16/5/11.
//  Copyright © 2016年 mondial-d. All rights reserved.
//

#import "FTImage.h"
// Foundation_Categories
#import "NSString+Verifying.h"
// FTValidation
#import "FTLibraries_FoundationValidation.h"

static NSString * const kWidth = @"width";
static NSString * const kHeight = @"height";
static NSString * const kPic = @"pic";

@interface FTImage ()
@property (assign, nonatomic, readwrite) CGFloat width;
@property (assign, nonatomic, readwrite) CGFloat height;
@property (strong, nonatomic, readwrite) NSURL *URL;
@end

@implementation FTImage
- (FTImage *)initWithDictionary:(NSDictionary *)dictionary
{
    if (self = [super init]) {
        self.width = [FTValidatedObject(dictionary[kWidth]) floatValue];
        self.height = [FTValidatedObject(dictionary[kHeight]) floatValue];
        self.URL = [NSURL URLWithString:FTValidatedString(dictionary[kPic])];
    }
    return self;
}

- (BOOL)isValidImage
{
    return (self.width > 0 && self.height > 0 && [self.URL description].length > 0);
}

- (instancetype)copyWithZone:(NSZone *)zone
{
    FTImage *image = [[[self class] allocWithZone:zone] init];
    image.width = _width;
    image.height = _height;
    image.URL = [_URL copy];
    
    return image;
}
+ (NSDictionary *)dictionaryWithFTImage:(FTImage *)ftImage
{
    if ([ftImage isValidImage]) {
        NSMutableDictionary *ftImageDict = [NSMutableDictionary dictionary];
        ftImageDict[kWidth] = @(ftImage.width);
        ftImageDict[kHeight] = @(ftImage.height);
        if (ftImage.URL) ftImageDict[kPic] = [ftImage.URL absoluteString];
        return ftImageDict;
    }
    else
        return nil;
}
@end
